﻿using Microsoft.AspNetCore.Mvc;
using SHRIS.MIS.Business.SystemManage;
using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Model.Result;
using SHRIS.MIS.Web.Controllers;
using SHRIS.MIS.Web.Filter;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SHRIS.MIS.Web.Areas.SystemManage.Controllers
{
    [Area("SystemManage")]
    public class DepartmentController : BaseController
    {
        #region 服务注入
        private DepartmentBLL _departmentBLL;

        public DepartmentController(DepartmentBLL departmentBLL)
        {
            _departmentBLL = departmentBLL;
        }
        #endregion

        #region 视图功能
        [AuthorizeFilter("system:department:view")]
        public IActionResult DepartmentIndex()
        {
            return View();
        }
        public IActionResult DepartmentForm()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("system:department:search,organization:user:search")]
        public async Task<IActionResult> GetListJson(DepartmentListParam param)
        {
            TEntity<List<DepartmentEntity>> obj = await _departmentBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("system:department:search,organization:user:search")]
        public async Task<IActionResult> GetDepartmentTreeListJson(DepartmentListParam param)
        {
            TEntity<List<ZtreeInfo>> obj = await _departmentBLL.GetZtreeDepartmentList(param);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetUserTreeListJson(DepartmentListParam param)
        {
            TEntity<List<ZtreeInfo>> obj = await _departmentBLL.GetZtreeUserList(param);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(Guid id)
        {
            TEntity<DepartmentEntity> obj = await _departmentBLL.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetMaxSortJson()
        {
            TEntity<int> obj = await _departmentBLL.GetMaxSort();
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("system:department:add,organization:department:edit")]
        public async Task<IActionResult> SaveFormJson(DepartmentEntity entity)
        {
            TEntity<string> obj = await _departmentBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("system:department:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TEntity obj = await _departmentBLL.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}